package com.x.xdweb.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class BeanUtilsIn 
{
	
	public static void copyProperties(Object dest, Object orig) throws Exception
	{
		Method[] methods = orig.getClass().getDeclaredMethods();
		
		List ltCopyTypes = new ArrayList();
		ltCopyTypes.add(String.class);
		ltCopyTypes.add(int.class);
		ltCopyTypes.add(boolean.class);
		ltCopyTypes.add(double.class);
		ltCopyTypes.add(byte.class);
		ltCopyTypes.add(float.class);
		ltCopyTypes.add(long.class);
		ltCopyTypes.add(BigDecimal.class);
		ltCopyTypes.add(BigInteger.class);
		ltCopyTypes.add(Integer.class);
		ltCopyTypes.add(Boolean.class);
		ltCopyTypes.add(Double.class);
		ltCopyTypes.add(Byte.class);
		ltCopyTypes.add(Float.class);
		ltCopyTypes.add(Long.class);
		ltCopyTypes.add(short.class);
		ltCopyTypes.add(Short.class);
		ltCopyTypes.add(java.sql.Date.class);
		ltCopyTypes.add(java.sql.Time.class);
		ltCopyTypes.add(java.sql.Timestamp.class);
		
		for(int i = 0; i < methods.length; i++)
		{
			if(methods[i].getName().indexOf("get") == 0 && ltCopyTypes.contains(methods[i].getReturnType()))
			{
				try {
					Method destMethod = dest.getClass().getMethod(methods[i].getName().replaceFirst("get", "set"), methods[i].getReturnType());
					destMethod.invoke(dest, methods[i].invoke(orig, null));
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					continue;
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
